package com.chagee.store.operation.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chagee.store.operation.api.bo.CommentExtDataBO;
import com.chagee.store.operation.api.enums.CommentTypeEnum;
import com.chagee.store.operation.server.config.EvaluateDictConfig;
import com.chagee.store.operation.server.entity.CommentConfigurationDO;
import com.chagee.store.operation.server.entity.StoreOrderEvaluateDO;
import com.chagee.store.operation.server.event.dao.CommentConfigurationMapper;
import com.chagee.store.operation.server.event.service.*;
import com.chagee.store.operation.server.model.info.EvaluateDictInfo;
import com.chagee.store.operation.server.model.param.EvaluateDetailParam;
import com.chagee.store.operation.server.model.param.EvaluateParam;
import com.chagee.store.operation.server.model.vo.CommentConfigurationVO;
import com.chagee.store.operation.server.model.vo.EvaluateDictResponse;
import com.personnel.auth.service.PersonnelAuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : hk
 * @Date : 2024/4/25
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class StoreAppEvaluateService {

	@Resource
	private TaskExecutionBaseService taskExecutionBaseService;
	@Resource
	private StoreTaskRelationBaseService storeTaskRelationBaseService;
	@Resource
	private BasicTaskConfigurationService basicTaskConfigurationService;
	@Resource
	private WorkOrderReceiverBaseService workOrderReceiverBaseService;
	@Resource
	private PersonnelAuthService personnelAuthService;
	@Resource
	private StoreOrderDeviceBaseService storeOrderDeviceBaseService;
	@Resource
	private WorkOrderBaseService workOrderBaseService;
	@Resource
	private StoreOrderEvaluateBaseService storeOrderEvaluateBaseService;


	@Resource
	private CommentConfigurationBaseService commentConfigurationBaseService;

	@Resource
	private CommentConfigurationMapper commentConfigurationMapper;

	public List<EvaluateDictResponse> evaluateDict() {
		//List<EvaluateDictInfo> evaluates = evaluateDictConfig.getEvaluates();
		List<CommentConfigurationDO> configurationDOS = commentConfigurationMapper.queryCommentConfig();
		List<EvaluateDictResponse> result = configurationDOS.stream().map(s -> {
			EvaluateDictResponse info = new EvaluateDictResponse();
			BeanUtils.copyProperties(s, info);
			//单选 ;多选
			if (s.getCommentType().equals(CommentTypeEnum.SINGLE_CHOICE.getType()) || s.getCommentType().equals(CommentTypeEnum.MULTIPLE_CHOICE.getType())){
				List<CommentExtDataBO> extDataBOS = JSONArray.parseArray(s.getExtData(), CommentExtDataBO.class);
				info.setExtData(extDataBOS);
			}
			return info;
		}).collect(Collectors.toList());
		return result;
	}


	public Boolean submitEvaluate(EvaluateParam param) {
		String storeCode = param.getStoreCode();
		String workOrderNumber = param.getWorkOrderNumber();
		//String evaluateScore = param.getEvaluateScore();
		List<EvaluateDetailParam> evaluateList = param.getEvaluateList();
		List<StoreOrderEvaluateDO> list = evaluateList.stream().map(s -> {
			StoreOrderEvaluateDO storeOrderEvaluateDO = new StoreOrderEvaluateDO();
			storeOrderEvaluateDO.setWorkOrderNumber(workOrderNumber);
			storeOrderEvaluateDO.setStoreCode(storeCode);
			storeOrderEvaluateDO.setEvaluate(s.getEvaluate());
			storeOrderEvaluateDO.setEvaluateCode(s.getEvaluateCode());
			storeOrderEvaluateDO.setEvaluateType(s.getEvaluateType());
			storeOrderEvaluateDO.setEvaluateResult(s.getEvaluateResult());
			storeOrderEvaluateDO.setRemark(s.getRemark());
			return storeOrderEvaluateDO;
		}).collect(Collectors.toList());
		return storeOrderEvaluateBaseService.saveBatch(list);
	}
}
